﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SDATA;
using DevComponents.DotNetBar.SuperGrid;

namespace SABI
{
    public partial class frm0125 : Form
    {
        string sql = "";
        bool isCheckAll = false;

        public frm0125()
        {
            InitializeComponent();
        }

        private void frm0125_Load(object sender, EventArgs e)
        {

        }

        private void frm0125_Shown(object sender, EventArgs e)
        {
            loadDATA_Combo();
        }

        void loadDATA_Combo()
        {
            sql = "SELECT DivisionID,DivisionName FROM tb0038 WHERE DISABLED = 0 ORDER BY DivisionID";
            TDBCboDivisionID.DataSource = DATA.GetDataTable(sql,FRM.ConnectString);
            TDBCboDivisionID.SelectedValue = FRM.DivisionID;

            sql = "SELECT ID,GroupID,GroupName FROM tb0070 WHERE DISABLED = 0 ORDER BY GroupID";
            TDBCboGroupID.DataSource = FRM.GetDataTable(sql);
            TDBCboGroupID.SelectedValue = FRM.GroupID;

            sql = "Select * From tb0077 Order by DataTypeID";
            TDBCboDataTypeID.DataSource = FRM.GetDataTable(sql);
        }

        void loadDATA_Grid1()
        {
            if (TDBCboGroupID.Text == "" || TDBCboDataTypeID.Text == "" || TDBCboDivisionID.Text=="")
            {
                GridDB1.PrimaryGrid.Rows.Clear();
                return;
            }
            switch(TDBCboDataTypeID.Text)
            {
                case "AC": sql = string.Format("SELECT ID,AccountID as DataID,AccountName as DataName,GroupShow,CASE WHEN CHARINDEX('@{0}',GroupShow)>0 THEN 1 ELSE 0 END AS IsCheck FROM tb0025 ORDER BY AccountID",TDBCboGroupID.SelectedItem.ItemData[0]);
                    break;
                case "BA": sql = string.Format("SELECT ID,BankAccountID as DataID,BankName as DataName,GroupShow,CASE WHEN CHARINDEX('@{0}',GroupShow)>0 THEN 1 ELSE 0 END AS IsCheck FROM tb0034 ORDER BY BankAccountID",TDBCboGroupID.SelectedItem.ItemData[0]);
                    break;
                case "IV": sql = string.Format("SELECT ID,InventoryID as DataID,InventoryName as DataName,GroupShow,CASE WHEN CHARINDEX('@{0}',GroupShow)>0 THEN 1 ELSE 0 END AS IsCheck FROM tb0054 ORDER BY InventoryID",TDBCboGroupID.SelectedItem.ItemData[0]);
                    break;
                case "OB": sql = string.Format("SELECT ID,ObjectID as DataID,ObjectName as DataName,GroupShow,CASE WHEN CHARINDEX('@{0}',GroupShow)>0 THEN 1 ELSE 0 END AS IsCheck FROM tb0045 ORDER BY ObjectID",TDBCboGroupID.SelectedItem.ItemData[0]);
                    break;
                case "RP": sql = string.Format("SELECT ID,ReportID as DataID,ReportName as DataName,GroupShow,CASE WHEN CHARINDEX('@{0}',GroupShow)>0 THEN 1 ELSE 0 END AS IsCheck FROM tb0189 ORDER BY ReportID",TDBCboGroupID.SelectedItem.ItemData[0]);
                    break;
                case "VT": sql = string.Format("SELECT ID,VoucherTypeID as DataID,VoucherTypeName as DataName,GroupShow,CASE WHEN CHARINDEX('@{0}',GroupShow)>0 THEN 1 ELSE 0 END AS IsCheck FROM tb0027 ORDER BY VoucherTypeID",TDBCboGroupID.SelectedItem.ItemData[0]);
                    break;
                case "PE": sql = string.Format("SELECT ID,MonthYear as DataID,[Quarter] as DataName,GroupShow,CASE WHEN CHARINDEX('@{0}',GroupShow)>0 THEN 1 ELSE 0 END AS IsCheck FROM vi0317 ",TDBCboGroupID.SelectedItem.ItemData[0]); // ORDER BY MonthYear
                    break;
                case "WA": sql = string.Format("SELECT ID,WareHouseID as DataID, WareHouseName as DataName,GroupShow,CASE WHEN CHARINDEX('@{0}',GroupShow)>0 THEN 1 ELSE 0 END AS IsCheck FROM tb0055 ", TDBCboGroupID.SelectedItem.ItemData[0]); // ORDER BY WareHouseID
                    break;
            }
            if (TDBCboDataTypeID.Text == "PE")
                sql = string.Format("{0} WHERE DivisionID='{1}' ORDER BY MonthYear ", sql, TDBCboDivisionID.Text);
            else if (TDBCboDataTypeID.Text == "WA")
                sql = string.Format("{0} WHERE DivisionID='{1}' ORDER BY WareHouseID ", sql, TDBCboDivisionID.Text);

            GridDB1.PrimaryGrid.DataSource = FRM.GetDataTable(sql);
            isCheckAll = false;
        }

        private void TDBCboDivisionID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtDivisionName.Text = TDBCboDivisionID.SelectedItem != null ? TDBCboDivisionID.SelectedItem.ItemData[1] : "";
            loadDATA_Grid1();
        }

        private void TDBCboGroupID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtGroupName.Text = TDBCboGroupID.SelectedItem!=null?TDBCboGroupID.SelectedItem.ItemData[2]:"";
            loadDATA_Grid1();
        }

        private void TDBCboDataTypeID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtDataTypeName.Text = TDBCboDataTypeID.SelectedItem != null ? TDBCboDataTypeID.SelectedItem.ItemData[1] : "";
            loadDATA_Grid1();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            (Application.OpenForms["frmHome"] as frmHOME).CloseTab(this);
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            GridDB1.Update();
            if (GridDB1.PrimaryGrid.Rows.Count == 0) { return; }
            isCheckAll = !isCheckAll;
            for (int i = 0; i < GridDB1.PrimaryGrid.Rows.Count; i++)
                GridDB1.GetCell(i, 3).Value = isCheckAll;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            GridDB1.Update();
            if (TDBCboGroupID.Text == "" || TDBCboDataTypeID.Text == "" || TDBCboDivisionID.Text == "") { return; }
            if (GridDB1.PrimaryGrid.VisibleRowCount == 0) { return; }
            string groupid = "@"+TDBCboGroupID.SelectedItem.ItemData[0];
            string sqlT = "";
            // tạo câu update
            switch (TDBCboDataTypeID.Text)
            {
                case "AC": sqlT = "UPDATE tb0025 SET ";
                    break;
                case "BA": sqlT = "UPDATE tb0034 SET ";
                    break;
                case "IV": sqlT = "UPDATE tb0054 SET ";
                    break;
                case "OB": sqlT = "UPDATE tb0045 SET ";
                    break;
                case "RP": sqlT = "UPDATE tb0189 SET ";
                    break;
                case "VT": sqlT = "UPDATE tb0027 SET ";
                    break;
                case "PE": sqlT = "UPDATE tb0199 SET ";
                    break;
                case "WA": sqlT = "UPDATE tb0055 SET ";
                    break;
            }

            pgBar.Visible = true;
            pgBar.Value = 0;
            pgBar.Maximum = GridDB1.PrimaryGrid.Rows.Count;

            for(int i=0; i<GridDB1.PrimaryGrid.Rows.Count;i++)
            {
                GridRow gr = (GridRow)GridDB1.PrimaryGrid.Rows[i];
                // kiểm tra lấy các dòng đã đc filter
                if (!gr.Visible) { continue; }
                string clID = gr.Cells["clID"].Value.ToString();
                string gshow = gr.Cells["clGroupShow"].Value.ToString();
                bool clIsCheck = gr.Cells["clIsCheck"].Value.ToString()=="1"?true:false;
                bool isUpdate = false;
                // kiểm tra có nên update hay ko
                if (clIsCheck && gshow.IndexOf(groupid) < 0) { 
                    isUpdate = true;
                    sql = sqlT + "GroupShow='" + gshow + groupid + "' WHERE [ID]='" + clID + "'";
                }
                else if (clIsCheck==false && gshow.IndexOf(groupid)>=0){
                    isUpdate = true;
                    sql = sqlT + "GroupShow='" + gshow.Replace(groupid, "") + "' WHERE [ID]='" + clID + "'";
                }
                if (!isUpdate) { continue; }
                else { DATA.ExecuteFAST(sql, FRM.ConnectString); }

                pgBar.Value = i;
                Application.DoEvents();
            }

            pgBar.Visible = false;
            Application.DoEvents();

            loadDATA_Grid1();
            SDLL.DLL02.Info("Cập nhật dữ liệu thành công");
        }

    }
}
